package com.lc.midi.mid;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MidiToMp3 {
	
	
	static boolean delFile(File file) {
        if (!file.exists()) {
            return false;
        }

        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                delFile(f);
            }
        }
        return file.delete();
    }

//	public static synchronized String midiToMp3(HttpServletRequest request, entity.Midi midi) throws InterruptedException, IOException {
//
//		String path = request.getSession().getServletContext().getRealPath("WEB-INF" + File.separatorChar + "temp");
//		String mp3Path = request.getSession().getServletContext().getRealPath("WEB-INF" + File.separatorChar + "mp3");
//		int midid = midi.getId();
//		File mp3Dir = new File(mp3Path);
//		if (!mp3Dir.exists()) {
//			mp3Dir.mkdirs();
//		}
//		File mp3File = new File(mp3Path, midid + ".mp3");
//		if (mp3File.exists()) {
//			return mp3File.getName();
//		}
//		File midFile = new File(path, midi.getName() + ".mid");
//		File midDir = new File(path);
//		if (midDir.exists()) {
//			delFile(midDir);
//		}
//		midDir.mkdirs();
//		FileOutputStream fos = new FileOutputStream(midFile);
//		fos.write(midi.getData());
//		fos.close();
//		String filename = midi.getName();
//		//filename = filename.replaceAll(" ", "\\ ");
//
//		String[] cmdWav1 = {"timidity", path + File.separatorChar + filename + ".mid", "-Ow", "-o", path + File.separatorChar + midid + ".wav"};
//		String[] cmdMp31 = {"ffmpeg", "-i", path + File.separatorChar + midid + ".wav",
//				"-acodec", "libmp3lame", "-ab", "256k", mp3Path + File.separatorChar + midid + ".mp3"};
//		//String cmdWav = "timidity " + path + File.separatorChar + filename + ".mid -Ow -o " + path + File.separatorChar + filename + ".wav";
//		//String cmdMp3 = "ffmpeg -i " + path + File.separatorChar + filename + ".wav -acodec libmp3lame -ab 256k " + path + File.separatorChar
//		//		+ filename + ".mp3";
//
//		Runtime.getRuntime().exec(cmdWav1).waitFor();
//		Runtime.getRuntime().exec(cmdMp31).waitFor();
//
//
//		return  midid + ".mp3";
//		//return filename + ".wav";
//	}

}
